package com.example.aacwebdemo.es2;

import com.github.javafaker.Faker;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;

/**
 * @author zhao.xin06679
 * @date 2021年06月08日 9:32
 */
@Document(indexName = "test_user_action_2")
public class EsBeanUserActionM {

    public EsBeanUserActionM(){}

    @Id
    private long id;
    //时间
    @Field(type = FieldType.Date,format = DateFormat.basic_date_time)
    private Date actionDate;

    //员工ID
    @Field(type = FieldType.Long)
    private Long staffID;

    //组织ID
    @Field(type = FieldType.Long)
    private Long entID;

    //部门ID
    @Field(type = FieldType.Nested)
    private Long[] sectorID;

    //分类:(通话/发内容/会议/日程)
    @Field(type = FieldType.Byte)
    private Byte actionType;

    //目标ID
    @Field(type = FieldType.Long,index = false)
    private Long targetID;

    //组织ID
    @Field(type = FieldType.Long,index = false)
    private Long targetEntID;

    //部门ID
    @Field(type = FieldType.Nested,index = false)
    private Long[] targetSectorID;

    //发内容公开：（公开/私人)
    @Field(type = FieldType.Byte)
    private Byte mPType;

    //发内容分类：（文字/图片/阅后既焚/文件传递/语音消息）
    @Field(type = FieldType.Byte)
    private Byte mType;

    //通话时长（秒）
    @Field(type = FieldType.Long,index = false)
    private Integer vTime;

    //会议ID
    @Field(type = FieldType.Long,index = false)
    private Long meetingID;

    //日程ID
    @Field(type = FieldType.Long,index = false)
    private Long scheduleID;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public Date getActionDate() {
        return actionDate;
    }

    public void setActionDate(Date actionDate) {
        this.actionDate = actionDate;
    }

    public Long getStaffID() {
        return staffID;
    }

    public void setStaffID(Long staffID) {
        this.staffID = staffID;
    }

    public Long getEntID() {
        return entID;
    }

    public void setEntID(Long entID) {
        this.entID = entID;
    }

    public Long[] getSectorID() {
        return sectorID;
    }

    public void setSectorID(Long[] sectorID) {
        this.sectorID = sectorID;
    }

    public Byte getActionType() {
        return actionType;
    }

    public void setActionType(Byte actionType) {
        this.actionType = actionType;
    }

    public Long getTargetID() {
        return targetID;
    }

    public void setTargetID(Long targetID) {
        this.targetID = targetID;
    }

    public Long getTargetEntID() {
        return targetEntID;
    }

    public void setTargetEntID(Long targetEntID) {
        this.targetEntID = targetEntID;
    }

    public Long[] getTargetSectorID() {
        return targetSectorID;
    }

    public void setTargetSectorID(Long[] targetSectorID) {
        this.targetSectorID = targetSectorID;
    }

    public Byte getmPType() {
        return mPType;
    }

    public void setmPType(Byte mPType) {
        this.mPType = mPType;
    }

    public Byte getmType() {
        return mType;
    }

    public void setmType(Byte mType) {
        this.mType = mType;
    }

    public Integer getvTime() {
        return vTime;
    }

    public void setvTime(Integer vTime) {
        this.vTime = vTime;
    }

    public Long getMeetingID() {
        return meetingID;
    }

    public void setMeetingID(Long meetingID) {
        this.meetingID = meetingID;
    }

    public Long getScheduleID() {
        return scheduleID;
    }

    public void setScheduleID(Long scheduleID) {
        this.scheduleID = scheduleID;
    }

    public static EsBeanUserActionM build(long i){

        Faker faker = new Faker(Locale.CHINA);

        EsBeanUserActionM bean = new EsBeanUserActionM();
        bean.setId(i);
        bean.setActionDate(new Date());


        byte actionType = ActionTypeM.RadmonBuild();

        bean.setActionType(actionType);

        bean.setStaffID(faker.random().nextLong(20000L));


        bean.setEntID(faker.random().nextLong(50L));
        bean.setSectorID(createSectorID());

        if (actionType == ActionTypeM.ht || actionType == ActionTypeM.message || actionType == ActionTypeM.meeting){
            bean.setTargetID(faker.random().nextLong(20000L));
            bean.setTargetEntID(faker.random().nextLong(50L));
            bean.setTargetSectorID(createSectorID());
        }


        if (actionType == ActionTypeM.ht){
            bean.setvTime(faker.random().nextInt(1,3600));


        } else if (actionType == ActionTypeM.message){
            bean.setmPType(MessageTypeM.RadmonBuild());
            bean.setmType(MessageTypeM.RadmonBuild());


        } else if (actionType == ActionTypeM.meeting){

            bean.setMeetingID(i);
        }else if (actionType == ActionTypeM.schedule){
            bean.setScheduleID(i);
        }else {
            throw new RuntimeException("action type is :" + actionType);
        }

        return bean;
    }

    public static Long[] createSectorID(){
        Faker faker = new Faker(Locale.CHINA);
        int size = faker.random().nextInt(1,5);
        Long[] result = new Long[size];
        Set<Long> tmpSet = new HashSet<>();

        for (int i = 0 ; i< size;i++){

            Long tmp = null;
            do{
                tmp = faker.random().nextLong(600);
            }while (tmpSet.contains(tmp));
            tmpSet.add(tmp);
            result[i] = tmp;
        }
        return result;
    }

}

class ActionTypeM {
    //通话/发内容/会议/日程
    public final static byte ht = 1;
    public final static byte message = 2;
    public final static byte meeting = 3;
    public final static byte schedule = 4;

    public static byte RadmonBuild(){
        Faker faker = new Faker(Locale.CHINA);
        int radmon = faker.random().nextInt(4);
        switch (radmon) {
            case 0:
                return ht;
            case 1:
                return message;
            case 2:
                return meeting;
            default:
                return schedule;

        }
    }

}

class MessageTypeM {
    //文字/图片/阅后既焚/文件传递/语音消息）
    public final static byte wz = 1;
    public final static byte tp = 2;
    public final static byte yhjf = 3;
    public final static byte wjzc = 4;
    public final static byte yixx = 5;

    public static byte RadmonBuild(){
        Faker faker = new Faker(Locale.CHINA);
        int radmon = faker.random().nextInt(5);
        switch (radmon) {
            case 0:
                return wz;
            case 1:
                return tp;
            case 2:
                return yhjf;
            case 3:
                return wjzc;
            default:
                return yixx;

        }
    }
}

class MessagePTypeM {
    //公开/隐私）
    public final static byte gk = 1;
    public final static byte ys = 2;


    public static byte RadmonBuild(){
        Faker faker = new Faker(Locale.CHINA);
        
        if (faker.random().nextBoolean()){
            return gk;
        }else {
            return ys;
        }
    }
}
